package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1. 在菜品表中根据name分页查询
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //准备封装查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //模糊查询
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);

        //执行菜品列表分页查询
        page = dishMapper.selectPage(page, wrapper);

        //2 遍历菜品列表
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //3. 根据菜品的分类id从分类表中查询分类信息(名称)
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());

            }
        }

        return page;
    }
    @Override
    public void save(Dish dish ){
        //向dish传递此此信息
        dishMapper.insert(dish);
        log.info("保存之后主键信息是：{}",dish.getId());

        //遍历前端的口味表
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isEmpty(flavorList)){
            for (DishFlavor dishFlavor:flavorList){
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }


    }
    @Override
    public Dish findById(Long id){
        Dish dish = dishMapper.selectById(id);
        //根据菜品id查分类信息
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());

        //根据菜品id从口味表查菜品
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavorList= dishFlavorMapper.selectList(wrapper1);
        dish.setFlavors(dishFlavorList);

        //返回
        return dish;
    }
    @Override
    public void update(Dish dish){
        //查询菜的基本信息
        dishMapper.updateById(dish);
        //删除口味表中的相关信息，然后重新加入
        LambdaQueryWrapper<DishFlavor>wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);

        //加入新的信息
        List<DishFlavor>  dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isEmpty(dishFlavorList)){
            for (DishFlavor dishFlavor : dishFlavorList){
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }

        }

    }
    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId)//判断
                .eq(Dish::getStatus, 1);//上架

        List<Dish> dishList = dishMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //根据菜品id获取口味
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }
    @Override
    public void updateByIds(Long [] ids,Integer status ){
        for (Long id : ids) {
            //update dish set status = #{} where id = #{}
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }

    }
    @Override
    public void  deleteByIds (Long [] ids){
        LambdaQueryWrapper<Dish>wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        wrapper.eq(Dish::getStatus,1);
        int count = dishMapper.selectCount(wrapper);
        if (count>0){
            throw new CustomException("菜品在售，无法删除");

        }
        for (Long id :ids){
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getId,id);
            dishFlavorMapper.delete(wrapper1);
        }
    }

}
